package requestManager;

import java.io.File;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import view.EmailApplicationViewImp;

import network.IpTableEntry;
import network.NetworkServerClientRequestHandler;

import model.Filter;
import model.EmailImp;
import model.EmailApplicationModelImp;
import model.MeetingInfo;
import model.Template;
import model.User;

/**
 * Email Application Controller component
 */
public class EmailApplicationRequestManager implements EmailApplicationRequestManagerImp
{
  
  private EmailApplicationModelImp model;
  
  private String user = "";
  
  private NetworkServerClientRequestHandler networkRequestHandler;
  
  private MessageReceivedCallback callback;
  
  private EmailApplicationViewImp emailAppView;

  /**
   * Email Application Controller constructor	
 * @param emailAppView the application view reference
 * @param networkRequestHandler the network request handler reference
   */
  public EmailApplicationRequestManager(EmailApplicationViewImp emailAppView, NetworkServerClientRequestHandler networkRequestHandler) 
  {
      this.networkRequestHandler = networkRequestHandler;
      this.callback = new MessageReceivedCallback(this);
      this.networkRequestHandler.addObserver(this.callback);
      this.emailAppView = emailAppView;
  }
  
  /*
   * e(non-Javadoc)
   * @see requestManager.EmailApplicationRequestManagerImp#registerModel(model.EmailApplicationModelImp)
   */
public void registerModel(EmailApplicationModelImp model) 
  {
	  this.model = model;
  }

  /*
   * (non-Javadoc)
   * @see requestManager.EmailApplicationRequestManagerImp#unRegiserModel()
   */
public void unRegiserModel() 
  {
	  this.model = null;
  }

 /*
  * (non-Javadoc)
  * @see requestManager.EmailApplicationRequestManagerImp#getModel()
  */
public EmailApplicationModelImp getModel()
  {
      return this.model;
  }
  
  /*
   * (non-Javadoc)
   * @see requestManager.EmailApplicationRequestManagerImp#requestPersitEmail(model.EmailImp, java.lang.String)
   */
public void requestPersitEmail(EmailImp eMail, String persistPath)
  {
      this.model.getPersistenceManager().persistEMail(eMail, persistPath);
  }

    /*
     * (non-Javadoc)
     * @see requestManager.EmailApplicationRequestManagerImp#requestPersitUser(model.User)
     */
	public void requestPersitUser(User user)
    {
      this.model.getPersistenceManager().persistUserAccount(user);
    } 
    
   /*
    * (non-Javadoc)
    * @see requestManager.EmailApplicationRequestManagerImp#requestFileDelete(java.lang.String)
    */
	public boolean requestFileDelete(String fileToDeletePath)
    {
      return this.model.getPersistenceManager().deleteFile(fileToDeletePath);
    }
    
    /*
     * (non-Javadoc)
     * @see requestManager.EmailApplicationRequestManagerImp#requestDeleteFiles(java.lang.String)
     */
	public void requestDeleteFiles(String path)
    {
        this.model.getPersistenceManager().deleteFiles(path);
    }
    
    /*
     * (non-Javadoc)
     * @see requestManager.EmailApplicationRequestManagerImp#requestMoveToFile(java.lang.String, java.lang.String)
     */
	public void requestMoveToFile(String sourceFilePath, String destrinationFilePath)
    {
        this.model.getPersistenceManager().moveToFile(sourceFilePath,destrinationFilePath);
    }
    
    /*
     * (non-Javadoc)
     * @see requestManager.EmailApplicationRequestManagerImp#requestCopyFile(java.lang.String, java.lang.String)
     */
	public void requestCopyFile(String sourceFilePath, String destrinationFilePath)
    {
        this.model.getPersistenceManager().copyFile(sourceFilePath, destrinationFilePath);
    }
    
    /*
     * (non-Javadoc)
     * @see requestManager.EmailApplicationRequestManagerImp#requestCreateFile(java.lang.String)
     */
	public void requestCreateFile(String path)
    {
        this.model.getPersistenceManager().createFile(path);
    }
    
    /*
     * (non-Javadoc)
     * @see requestManager.EmailApplicationRequestManagerImp#requestEmailFilesFromFolder(java.lang.String)
     */
	public List<File> requestEmailFilesFromFolder(String path)
    {
        return this.model.getPersistenceManager().getEmailFilesFromFolder(path);
    }
    
    /*
     * (non-Javadoc)
     * @see requestManager.EmailApplicationRequestManagerImp#requestPersistTemplate(model.Template, java.lang.String)
     */
	public void requestPersistTemplate(Template template, String path)
    {
        this.model.getPersistenceManager().writeTemplateAsFile(template, path);
    }
    
    /*
     * (non-Javadoc)
     * @see requestManager.EmailApplicationRequestManagerImp#requestMeetingInfo(java.lang.String)
     */
	public MeetingInfo requestMeetingInfo(String meetingPath) 
    {
        return this.model.getPersistenceManager().getMeetingInfo(meetingPath);
    }
    
    /*
     * (non-Javadoc)
     * @see requestManager.EmailApplicationRequestManagerImp#requestPersistFilter(model.Filter, java.lang.String)
     */
	public void requestPersistFilter(Filter filter, String filterPath)
    {
        this.model.getPersistenceManager().writeFilterAsFile(filter,filterPath);
    }    
    
    /*
     * (non-Javadoc)
     * @see requestManager.EmailApplicationRequestManagerImp#requestEmailFilesDeepInsideFolder(java.lang.String)
     */
	public List<File> requestEmailFilesDeepInsideFolder(String path)
    {
        return this.model.getPersistenceManager().getEmailFilesDeepInsideFolder(path);
    }
    
    /*
     * (non-Javadoc)
     * @see requestManager.EmailApplicationRequestManagerImp#requestIsEmailIdRegistered(java.lang.String)
     */
	public boolean requestIsEmailIdRegistered(String emailId)
    {
        return this.model.getPersistenceManager().isEmailIdRegistered(emailId);
    }
    
    /*
     * (non-Javadoc)
     * @see requestManager.EmailApplicationRequestManagerImp#requestIsValidUser(java.lang.String, java.lang.String)
     */
	public boolean requestIsValidUser(String emailID, String password)
    {
        return this.model.getPersistenceManager().isValidUser(emailID,password);
    }
    
    /*
     * (non-Javadoc)
     * @see requestManager.EmailApplicationRequestManagerImp#requestSearchResult(java.lang.String, java.lang.String, boolean)
     */
	public List<File> requestSearchResult(String user, String query, boolean isRegEx)
    {
        return this.model.getPersistenceManager().getSearchResult(user,query, isRegEx);
    }
  
    /*
     * (non-Javadoc)
     * @see requestManager.EmailApplicationRequestManagerImp#requestAdvancedSearchResult(java.lang.String, java.lang.String, int)
     */
	public Map<String,File> requestAdvancedSearchResult(String user, String query, int SearchType)
    {
    	return this.model.getPersistenceManager().getAdvancedSearchResult(user, query, SearchType);
    }
    
    /*
     * (non-Javadoc)
     * @see requestManager.EmailApplicationRequestManagerImp#requestTemplatesFromPersistence(java.lang.String)
     */
	public List<Template> requestTemplatesFromPersistence(String path)
    {
        return this.model.getPersistenceManager().getTemplateFromFolder(path);
    }
    
    /*
     * (non-Javadoc)
     * @see requestManager.EmailApplicationRequestManagerImp#requestFiltersFromPersistence(java.lang.String)
     */
	public List<Filter> requestFiltersFromPersistence(String path)
    {
        return this.model.getPersistenceManager().getFilterFromFolder(path);
    }
    
    /*
     * (non-Javadoc)
     * @see requestManager.EmailApplicationRequestManagerImp#loginSuccess(java.lang.String)
     */
	public void loginSuccess(String emailId)
    {
        this.user = emailId;
        this.networkRequestHandler.updateWith(new IpTableEntry(this.user,NetworkServerClientRequestHandler.ip()));
        this.networkRequestHandler.runServer();
    }
    
    /*
     * (non-Javadoc)
     * @see requestManager.EmailApplicationRequestManagerImp#getUser()
     */
	public String getUser()
    {
        return this.user;        
    }
    
    /*
     * (non-Javadoc)
     * @see requestManager.EmailApplicationRequestManagerImp#mergeAndConvertToList(java.util.Map, java.util.Map, java.util.Map)
     */
	public List<File> mergeAndConvertToList(Map<String,File> mapOne, Map<String,File> mapTwo,Map<String,File> mapThree )
    {
    	Map<String,File> merged = new HashMap<String,File>();
    	List<File> mergedList = new LinkedList<File>();
    	
    	if(!mapOne.isEmpty())
    		{
    			merged.putAll(mapOne);
    		}
    	if(!mapTwo.isEmpty())
    		{
    			merged.putAll(mapTwo);
    		}
    	if(!mapThree.isEmpty())
    		{
    			merged.putAll(mapThree);
    		}
    	
    	if(!merged.isEmpty())
    		mergedList = new LinkedList<File>(merged.values());
		
    		
    	return mergedList;
    }

    /*
     * (non-Javadoc)
     * @see requestManager.EmailApplicationRequestManagerImp#mailReceivedInOutbox(java.lang.String)
     */
    @Override
    public void emailReceivedToOutbox(final String emailFilePath)
    {
         EmailImp email = EmailApplicationRequestManager.this.emailAppView.getEmailFromJasonFile(new File(emailFilePath));   
         EmailApplicationRequestManager.this.networkRequestHandler.sendEmailThroughServer(email); 
    }

	/*
	 * (non-Javadoc)
	 * @see requestManager.EmailApplicationRequestManagerImp#requestEmailFromJson(java.lang.String)
	 */
	public EmailImp requestEmailFromJson(String path) 
	{
		return this.model.getPersistenceManager().emailJsonToEmailImp(path);
	}
	
	/*
	 * (non-Javadoc)
	 * @see requestManager.EmailApplicationRequestManagerImp#getNetworkHandler()
	 */
	public NetworkServerClientRequestHandler getNetworkHandler()
	    {
	        return this.networkRequestHandler;
	    }
	   
	/*
	 * (non-Javadoc)
	 * @see requestManager.EmailApplicationRequestManagerImp#requestPersistMeetingInfo(model.MeetingInfo, java.lang.String)
	 */
	public void requestPersistMeetingInfo(MeetingInfo info, String path)
       {
	        this.model.getPersistenceManager().persistMeetingInfo(info,path);
       }

    /*
     * (non-Javadoc)
     * @see requestManager.EmailApplicationRequestManagerImp#requestAllMeetingInfo(java.lang.String)
     */
    public List<MeetingInfo> requestAllMeetingInfo(String rootFolderPath)
    {
        return this.model.getPersistenceManager().getPersistMeetingInfo(rootFolderPath);
    }


}